/**
 * 
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");

	var managementSup = {
		/**
		 * bindEvent 页面事件
		 */
		type: "",
		currentPage: 1,
		pageCount: 10,
		loadedAllFlag: false,
		bindEvent: function() {
			//返回
			$("#return_supmana").unbind().on("click", function() {
				window.history.back(-1);
			});

			$("#filter_icon").unbind().on("click", function() {
				$(".searchInput").val("");
				managementSup.getJunior();
				managementSup.getMemberLevel();
				if($(".filter_content").css("left") === "0px") {
					$(".filter_content").animate({
						left: '100%'
					});
					$(".ischecked").removeClass("ischecked");
					$(".btn").html("");
					$(".filter_container .scroller_iscroll").css("transform", "translate(0px,0px) translateZ(0px)");
				} else {
					$(".filter_content").animate({
						left: '0px'
					});
				}
			});
			//搜索
			
			$(".searchkey").unbind().on("click", function() {
				$("#menbers_managementSup").html("");
				managementSup.currentPage = 1;
				managementSup.loadedAllFlag = false;
				managementSup.sendParam(managementSup.supertype);
			});
			$(".replace_btn").unbind().on("click", function() {
				$(".filter_container .list ul li .ischecked").removeClass("ischecked");
				$(".filter_container .member_docker .list .btn").html("");
			});
			$(".sure_btn").unbind().on("click", function() {;
				managementSup.screeningMember();

				$(".filter_content").animate({
					left: '100%'
				});
				$(".filter_container .list ul li .ischecked").removeClass("ischecked");
				$(".filter_container .member_docker .list .btn").html("");
				$(".filter_container .scroller_iscroll").css("transform", "translate(0px,0px) translateZ(0px)");

			});
			$(".input_sea").unbind().on("click", function() {
				window.location.href = "#/index/search";
			});
			
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: true,
					from: "left"
				});
			});
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			Path.listen();
		},

		/**
		 * 页面数据加载
		 */
		sendParam: function(type) {
			var searchKey = $(".searchInputM").val();
			var docker = [];
			var currentLevel = [];
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"type": "2", //2代表正式会员，这里传正式会员参数
				"pk_user": pk_user,
				"currentPage": managementSup.currentPage,
				"pageCount": managementSup.pageCount,
				"searchKey": searchKey,
				"docker": docker,
				"currentLevel": currentLevel
			}
			common.loadData({
				url: "/service/RRTPubServlet?method=screeningMember", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: managementSup.getMemListCallBack, //回调函数
				loadAll: managementSup.loadedAllFlag,
				cache: false
			});
		},
		getJunior: function() {
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"pk_user": pk_user
			}
			console.log(param);
			common.loadData({
				url: "/service/RRTPubServlet?method=getNextPsndoc", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: managementSup.getJuniorCallBack, //回调函数
				cache: false
			});
		},
		getMemberLevel: function(){
			var pk_user = window.localStorage.getItem("pk_user");
			var param = {
				"pk_user": pk_user

			}
			common.loadData({
				url: "/service/RRTPubServlet?method=initialize", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: managementSup.getMemberLevelCallBack, //回调函数
				dataSource: "Login",
				cache: false
			});
		},
		getMemberLevelCallBack: function(json){
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				var html = "";
				for(var i = 0; i < json.data.level.length; i++) {
					html += '<li class="lf"><div class="grade" memopk="'+
					json.data.level[i].pk_defdoc+'">'
						+ json.data.level[i].name
						+'</div></li>';
				}
				$(".memberlevel").html(html);
				$(".grade").unbind().on("click", function() {
					if($(this).hasClass("ischecked")) {
						$(this).removeClass("ischecked");
					} else {
						$(this).addClass("ischecked");
					}
				});
				var initializestr = JSON.stringify(json.data);
				//刷新本地初始化数据
				window.localStorage.setItem("initialize",initializestr);
				
			} else {
				common.showTip({
					content: json.message
				});
			}
		},
		getJuniorCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				//展示页面
				var html = "";
				for(var i = 0; i < json.data.list.length; i++) {
					html += '<li><span class="name" datatype="'
						+json.data.list[i].pk_psndoc
						+'">' +
						json.data.list[i].name +
						'</span><span class="btn"></span></li>';
				}
				$(".junior").html(html);
				$(".btn").unbind().on("click", function() {
					if($(this).hasClass("ischecked")) {
						$(this).removeClass("ischecked");
						$(this).html("");
					} else {
						$(this).addClass("ischecked");
						$(this).html("√");
					}
				});
			} else {
				common.showTip({
					content: json.message
				});
			}
		},
		screeningMember: function() {
			var searchKey = "";
			var pk_user = window.localStorage.getItem("pk_user");
			var clist = $(".grade");
			var dlist = $(".btn")
			var docker = [];
			var currentLevel = [];
			for(var i = 0; i < dlist.length; i++) {
				if($(dlist[i]).hasClass("ischecked")) {
					docker.push($(dlist[i]).siblings().attr("datatype"));
					console.log(docker);
				}
			}
			for(var j = 0; j < clist.length; j++) {
				if($(clist[j]).hasClass("ischecked")) {
					currentLevel.push($(clist[j]).attr("memopk"));
					console.log(currentLevel);
				}
			}
			var param = {
				"pk_user": pk_user,
				"docker": docker,
				"currentLevel": currentLevel,
				"currentPage": managementSup.currentPage,
				"pageCount": managementSup.pageCount,
				"searchKey": searchKey
			}
			console.log(param);
			common.loadData({
				url: "/service/RRTPubServlet?method=screeningMember", //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: managementSup.screeningMemberCallBack, //回调函数
				dataSource: "screeningMember",
				cache: false
			});
		},

		loadData: function(currentPage) {
			//接口数据
			managementSup.currentPage = currentPage;
			managementSup.sendParam(managementSup.type);
			console.log(managementSup.type);

		},
		getMemListCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				//展示页面
				var html = "";
				for(var i = 0; i < json.data.list.length; i++) {
					html += " <div class='men_mana men_ship public tiaozhuan' pk_member='" +
						json.data.list[i].pk_member +
						"'>" +
						' <div class="sp_img lf">' +
						' <img src="images/' + json.data.list[i].besystem + '.png" alt="">' +
						' </div>' +
						' <div class="sp_item rf">' +
						' <div class="h_top">' +
						' <span class="lf"><b>' + json.data.list[i].name + '</b>-<b>' + json.data.list[i].currentLevel + '</b></span>' +
						' <span class="rf mana_time">' + json.data.list[i].createTime + '</span>' +
						' </div><div class="h_bottom">' +
						' <span class="lf"><b>对接人</b> : <b>' + json.data.list[i].docker + '</b></span>' +
						' <a class="rf tel" href="tel:'+ json.data.list[i].tel + '">打电话</a>' +
						' </div></div></a></div>';
				}
				$("#menbers_managementSup").append(html);
				$(".tiaozhuan").unbind().on("click", function(e) {
					var pk_member = $(this).attr("pk_member");
					window.localStorage.setItem("pk_member", pk_member);
					if(e.target.tagName!="A"){
						window.location.href = "#/index/memberShip";	
					}
				})
				//把type传入本地
				var type = window.localStorage.setItem("type", 1);

				var iscroll = require("iscroll");
				common.initIscroll({
					container: "iscrollContent_managementSup",
					showScrollBar: true,
					pullUpActionCallBack: managementSup.pullUpActionCallBack,
					pullDownActionCallBack: managementSup.pullDownActionCallBack,
					loadAll: managementSup.loadedAllFlag
				});

			} else {
				common.showTip({
					content: json.message
				});
			}
		},
		screeningMemberCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				$("#menbers_managementSup").empty();
				var html = "";
				for(var i = 0; i < json.data.list.length; i++) {
					html += " <div class='men_mana pending public tiaozhuan' pk_member='" +
						json.data.list[i].pk_member +
						"'>" +
						" <a href='#/index/men_details'>" +
						"<div class='sp_img lf'>" +
						' <img src="images/' + json.data.list[i].besystem + '.png" alt="">' +
						"  </div>" +
						" <div class='sp_item rf'>" +
						"		<div class='h_top'>" +
						"			<span class='lf'><b>" + json.data.list[i].name + "</b>-<b>银卡会员</b></span>" +
						"			<span class='rf mana_time'>" + json.data.list[i].createTime + "</span>" +
						"		</div>" +
						"		<div class='h_bottom'>" +
						"			<span class='lf'><b>对接人</b> : <b>" + json.data.list[i].docker + "</b></span>" +
						"			<a class='rf tel' href='tel:" + json.data.list[i].tel + "'>打电话</a>" +
						"		</div>" +
						"	</div>" +
						"	</a>" +
						"	</div>";
				}
				$("#menbers_managementSup").append(html);
			}else if(json.status == "1"){
				
			}else if(json.status == "2"){
				window.location.href = "#/index/login";
			} else {
				common.showTip({
					content: json.message
				});
			}
		},

		pullUpActionCallBack: function() {
			managementSup.currentPage += 1;
			managementSup.loadData(managementSup.currentPage);
		},
		pullDownActionCallBack: function() {
			managementSup.currentPage = 1;
			//继续加载页面
			$("#menbers_managementSup").html("");

			managementSup.loadedAllFlag = false;
			//common.continueLoad();
			managementSup.loadData(managementSup.currentPage);
		},
		/**
		 * each page init mothed
		 */
		initialize: function() {

			this.loadData(this.currentPage = 1);
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = managementSup;
});